package speedball.popup;
//@ViewController(value = "/fxml/ui/ScrollPane.fxml", title = "Material Design Example")

import javafx.application.Platform;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.transform.Scale;
import javafx.stage.Stage;
import speedball.notice.SocketService;
import speedball.skincenter.SkinCenterStage;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.MenuItem;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URL;
import java.util.ResourceBundle;

public class MenuController implements Initializable {

    private SocketService socketService;
    @FXML
    private ScrollPane scrollPane;

    private Pane parent;

    private Stage parentStage;

    private Stage skinCenterStage;
    @FXML
    private Button addButton;
    @FXML
    private Button subButton;
    @FXML
    private Button setButton;
    @FXML
    private MyToggleButton mindWarnButton;

    @FXML
    private MyToggleButton alwaysOnTopButton;
    @FXML
    private MyToggleButton showExpandButton;
    @FXML
    private MyToggleButton closeButton;
    @FXML
    private MyToggleButton autoPaddingButton;
    @FXML
    private MyToggleButton autoGameModeButton;
    @FXML
    private ToggleButton scale1;
    @FXML
    private ToggleButton scale2;
    @FXML
    private ToggleButton scale3;
    @FXML
    private ToggleButton scale4;

    @FXML
    private TextField opacityText;

    @FXML
    private Button showButton;
    @FXML
    private Label showLabel;

    @FXML
    private void handleShowButtonAction(ActionEvent event) {
        System.out.println("656666ewqewqewqeq6666666");

        System.out.println();
//        parentStage.setAlwaysOnTop(alwaysOnTopButton.isSelected());
//        System.out.println(parentStage.isAlwaysOnTop());

    }
    @FXML
    private void handleAlwaysOnTopButtonAction(ActionEvent event) {
        System.out.println("656666666666666666666");
        System.out.println(alwaysOnTopButton.isSelected());
        parentStage.setAlwaysOnTop(alwaysOnTopButton.isSelected());
        System.out.println(parentStage.isAlwaysOnTop());

    }
    @FXML
    private void handleCloseButtonAction(ActionEvent event) {
        System.out.println(closeButton.isSelected());
        if(closeButton.isSelected()&&parentStage.isShowing()){
            parentStage.close();
        }
    }

    @FXML
    private void handleAutoGameModeButtonAction(ActionEvent event) {
        System.out.println("autoGameModeButton:"+autoGameModeButton.getText());
        System.out.println(autoGameModeButton.isSelected());
//        if(closeButton.isSelected()&&parentStage.isShowing()){
//            parentStage.close();
//        }
        if(autoGameModeButton.isSelected()){
            if(!socketService.isOpening()){
                socketService.open();
            }
            socketService.acceptDeal();
        }else {
            if(socketService.isOpening()){
                socketService.close();
            }
        }
    }
    @FXML
    private void handleAutoPaddingButtonAction(ActionEvent event) {
        System.out.println(autoPaddingButton.isSelected());

        parent.setUserData(autoPaddingButton.isSelected());

//        if(closeButton.isSelected()&&parentStage.isShowing()){
//            parentStage.close();
//        }
    }

    @FXML
    private void handleMindWarnButtonAction(ActionEvent event) {
        System.out.println(mindWarnButton.isSelected());



//        if(closeButton.isSelected()&&parentStage.isShowing()){
//            parentStage.close();
//        }
    }
    @FXML
    private void handleShowExpandButtonAction(ActionEvent event) {
        System.out.println(showExpandButton.isSelected());




//        if(closeButton.isSelected()&&parentStage.isShowing()){
//            parentStage.close();
//        }
    }



    @FXML
    private void handleAddButtonAction(ActionEvent event) {
        BigDecimal sd= new BigDecimal(parent.getOpacity()+0.05D).setScale(2, BigDecimal.ROUND_HALF_UP);
        if(sd.doubleValue()<=1.00D){
            parent.setOpacity(sd.doubleValue());
            opacityText.setText(String.valueOf((int)(sd.doubleValue()*100))+"%");
        }
        if(parent.getOpacity()==1.00D){
            addButton.setDisable(true);
        }
        if(parent.getOpacity()!=0.25D&& subButton.isDisabled()){
            subButton.setDisable(false);
        }
    }
    @FXML
    private void handleSubButtonAction(ActionEvent event) {
        System.out.println(parent.getOpacity());

        BigDecimal sd= new BigDecimal(parent.getOpacity()-0.05D).setScale(2, BigDecimal.ROUND_HALF_UP);

        if(sd.doubleValue()>=0.25D){
            parent.setOpacity(sd.doubleValue());
            opacityText.setText(String.valueOf((int)(sd.doubleValue()*100))+"%");
        }
        System.out.println(parent.getOpacity());
        if(parent.getOpacity()==0.25D){
            subButton.setDisable(true);
        }
        if(parent.getOpacity()!=1.00D&&addButton.isDisabled()){
            addButton.setDisable(false);
        }
    }

    @FXML
    private void handleskinButtonAction(ActionEvent event) {

if(!skinCenterStage.isShowing()){
    skinCenterStage.show();
}
    }

    @FXML
    private void handleReturnButtonAction(ActionEvent event) {
        scrollPane.setHvalue(0);
    }

    @FXML
    private void handleSetButtonAction(ActionEvent event) {
        scrollPane.setHvalue(1);
    }
    @FXML
    private void handleLocationButtonAction(ActionEvent event) throws IOException {

        PopupMenu pop = new PopupMenu();//创建弹出式菜单

        MenuItem menu = new MenuItem("退出");//创建菜单项
        menu.addActionListener(new ActionListener() {
            //给菜单项添加事件监听器，单击时退出系统

            @Override
            public void actionPerformed(java.awt.event.ActionEvent e) {
                // TODO Auto-generated method stub
//                System.exit(0);
            }
        });
        pop.add(menu);


//添加系统托盘图标.
        SystemTray tray = SystemTray.getSystemTray();
        BufferedImage image = ImageIO.read(MenuController.class
                .getResourceAsStream("../../img/c10_20x20.png"));
        TrayIcon trayIcon = new TrayIcon(image, "自动备份工具", pop);
        trayIcon.setToolTip("加速器");
        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            e.printStackTrace();
        }
//        parentStage.close();
        parentStage.hide();

        trayIcon.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {

                System.out.println("点击了左键");
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
//                        parentStage.
                        System.out.println("2111111111111111");
                        parentStage.show();
                        System.out.println("parentStage.show():" + parentStage.isShowing());
                        //更新JavaFX的主线程的代码放在此处
                    }
                });
                tray.remove(trayIcon);
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });

//        Platform.runLater(new Runnable() {
//            @Override
//            public void run() {
//                trayIcon.addMouseListener(new MouseListener(){
//                    @Override
//                    public void mouseClicked(MouseEvent e) {
//                        System.out.println("点击了左键");
//
////                        parentStage.
//                        System.out.println("2111111111111111");
//                        parentStage.show();
//                        parentStage.hide();
//                        System.out.println("parentStage.show():"+parentStage.isShowing());
//                        //更新JavaFX的主线程的代码放在此处
//
//
//                        tray.remove(trayIcon);
//                    }
//
//                    @Override
//                    public void mousePressed(MouseEvent e) {
//
//                    }
//
//                    @Override
//                    public void mouseReleased(MouseEvent e) {
//
//                    }
//
//                    @Override
//                    public void mouseEntered(MouseEvent e) {
//
//                    }
//
//                    @Override
//                    public void mouseExited(MouseEvent e) {
//
//                    }
//                }); }
//        });
//



    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
//在点击左边的按钮时，将ScrollPane的HValue设置为0，使滚动条滚动到最左边
//在点击右边的按钮时，将ScrollPane的HValue设置为1，使滚动条滚动到最右边
        //btnLeft.getStyleClass().add("transparent");
//        scrollPane.getStyleClass().add("transparent");
        scrollPane.setStyle("-fx-background-color: transparent;-fx-control-inner-background: transparent;");
        scrollPane.setPannable(false);
        scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
        scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);





        final ToggleGroup group = new ToggleGroup();
        scale1.setUserData(1.0D);
        scale2.setUserData(1.1D);
        scale3.setUserData(1.2D);
        scale4.setUserData(1.3D);
        scale1.setToggleGroup(group);
        scale2.setToggleGroup(group);
        scale3.setToggleGroup(group);
        scale4.setToggleGroup(group);
        group.selectedToggleProperty().addListener(
                (ObservableValue<? extends Toggle> ov,
                Toggle toggle, Toggle new_toggle) -> {
                    System.out.println("click ToggleGroup");
            if (new_toggle == null){
                System.out.println(toggle.getUserData());
//                rect.setFill(Color.WHITE);
//                    group.setSelectedToggle(toggle);
                    group.selectToggle(toggle);
            }
            else{
//                rect.setFill((Color) group.getSelectedToggle().getUserData());
                System.out.println(group.getSelectedToggle().getUserData());
//                Scale scale=new Scale(0,0,(double)new_toggle.getUserData(),(double)new_toggle.getUserData());
                parent.setScaleX((double)new_toggle.getUserData());
                parent.setScaleY((double)new_toggle.getUserData());
                parent.setMinWidth(1000D);
                parent.setMinHeight(600D);
                parent.setLayoutX(0);
                parent.setLayoutY(0);
//                parent.getTransforms().add(scale);
                System.out.println(parent.getWidth());
//                System.out.println( parentStage.isResizable());
//                parentStage.setWidth(1000D);
//                parentStage.setHeight(800D);

            }
        });


//        scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);

    }

    public Node getParent() {
        return parent;
    }

    public void setParent(Pane parent) {

        if(parent.getOpacity()==0.25D&& !subButton.isDisabled()){
            subButton.setDisable(true);
        }

        if(parent.getOpacity()==1.00D&&!subButton.isDisabled()){
            addButton.setDisable(true);
        }

        opacityText.setText(String.valueOf((int)(parent.getOpacity()*100))+"%");
        opacityText.setDisable(true);
        this.parent = parent;
    }

    public Stage getSkinCenterStage() {
        return skinCenterStage;
    }

    public void setSkinCenterStage(Stage skinCenterStage) {
        this.skinCenterStage = skinCenterStage;
    }

    public ScrollPane getScrollPane() {
        return scrollPane;
    }

    public void setScrollPane(ScrollPane scrollPane) {
        this.scrollPane = scrollPane;
    }

    public Stage getParentStage() {
        return parentStage;
    }

    public void setParentStage(Stage parentStage) {

        alwaysOnTopButton.setSelected(parentStage.isAlwaysOnTop());
        System.out.println("parentStage.isShowing()" +parentStage.isShowing());
        closeButton.setSelected(parentStage.isShowing());
        autoPaddingButton.setSelected(true);
        autoGameModeButton.setSelected(true);
        mindWarnButton.setSelected(true);
        showExpandButton.setSelected(true);
        this.parentStage = parentStage;
    }

    public Button getShowButton() {
        return showButton;
    }

    public void setShowButton(Button showButton) {
        this.showButton = showButton;
    }

    public Label getShowLabel() {
        return showLabel;
    }

    public void setShowLabel(Label showLabel) {
        this.showLabel = showLabel;
    }

    public Button getAddButton() {
        return addButton;
    }

    public void setAddButton(Button addButton) {
        this.addButton = addButton;
    }

    public Button getSubButton() {
        return subButton;
    }

    public void setSubButton(Button subButton) {
        this.subButton = subButton;
    }

    public Button getSetButton() {
        return setButton;
    }

    public void setSetButton(Button setButton) {
        this.setButton = setButton;
    }

    public MyToggleButton getMindWarnButton() {
        return mindWarnButton;
    }

    public void setMindWarnButton(MyToggleButton mindWarnButton) {
        this.mindWarnButton = mindWarnButton;
    }

    public MyToggleButton getAlwaysOnTopButton() {
        return alwaysOnTopButton;
    }

    public void setAlwaysOnTopButton(MyToggleButton alwaysOnTopButton) {
        this.alwaysOnTopButton = alwaysOnTopButton;
    }

    public MyToggleButton getShowExpandButton() {
        return showExpandButton;
    }

    public void setShowExpandButton(MyToggleButton showExpandButton) {
        this.showExpandButton = showExpandButton;
    }

    public MyToggleButton getCloseButton() {
        return closeButton;
    }

    public void setCloseButton(MyToggleButton closeButton) {
        this.closeButton = closeButton;
    }

    public MyToggleButton getAutoPaddingButton() {
        return autoPaddingButton;
    }

    public void setAutoPaddingButton(MyToggleButton autoPaddingButton) {
        this.autoPaddingButton = autoPaddingButton;
    }

    public MyToggleButton getAutoGameModeButton() {
        return autoGameModeButton;
    }

    public void setAutoGameModeButton(MyToggleButton autoGameModeButton) {
        this.autoGameModeButton = autoGameModeButton;
    }

    public ToggleButton getScale1() {
        return scale1;
    }

    public void setScale1(ToggleButton scale1) {
        this.scale1 = scale1;
    }

    public ToggleButton getScale2() {
        return scale2;
    }

    public void setScale2(ToggleButton scale2) {
        this.scale2 = scale2;
    }

    public ToggleButton getScale3() {
        return scale3;
    }

    public void setScale3(ToggleButton scale3) {
        this.scale3 = scale3;
    }

    public ToggleButton getScale4() {
        return scale4;
    }

    public void setScale4(ToggleButton scale4) {
        this.scale4 = scale4;
    }

    public TextField getOpacityText() {
        return opacityText;
    }

    public void setOpacityText(TextField opacityText) {
        this.opacityText = opacityText;
    }

    public SocketService getSocketService() {
        return socketService;
    }

    public void setSocketService(SocketService socketService) {
        this.socketService = socketService;
    }

    class MenuVariable{

        private boolean  mindWarn;


        private boolean alwaysOnTop;

        private boolean showExpand;

        private boolean close;

        private boolean autoPadding;

        private boolean autoGameMode;

        private double scale1;

        private double scale2;

        private double scale3;

        private double scale4;

        public boolean isMindWarn() {
            return mindWarn;
        }

        public void setMindWarn(boolean mindWarn) {
            this.mindWarn = mindWarn;
        }

        public boolean isAlwaysOnTop() {
            return alwaysOnTop;
        }

        public void setAlwaysOnTop(boolean alwaysOnTop) {
            this.alwaysOnTop = alwaysOnTop;
        }

        public boolean isShowExpand() {
            return showExpand;
        }

        public void setShowExpand(boolean showExpand) {
            this.showExpand = showExpand;
        }

        public boolean isClose() {
            return close;
        }

        public void setClose(boolean close) {
            this.close = close;
        }

        public boolean isAutoPadding() {
            return autoPadding;
        }

        public void setAutoPadding(boolean autoPadding) {
            this.autoPadding = autoPadding;
        }

        public boolean isAutoGameMode() {
            return autoGameMode;
        }

        public void setAutoGameMode(boolean autoGameMode) {
            this.autoGameMode = autoGameMode;
        }

        public double getScale1() {
            return scale1;
        }

        public void setScale1(double scale1) {
            this.scale1 = scale1;
        }

        public double getScale2() {
            return scale2;
        }

        public void setScale2(double scale2) {
            this.scale2 = scale2;
        }

        public double getScale3() {
            return scale3;
        }

        public void setScale3(double scale3) {
            this.scale3 = scale3;
        }

        public double getScale4() {
            return scale4;
        }

        public void setScale4(double scale4) {
            this.scale4 = scale4;
        }
    }

}


//    public void Tray() throws AWTException{
//
//        if(SystemTray.isSupported()) {//判断系统是否支持托盘图标
//
//            URL resource = this.getClass().getResource("/black.jpg");//获取托盘图标
//            ImageIcon icon = new ImageIcon(resource);
//            PopupMenu pop = new PopupMenu();//创建弹出式菜单
//
//            MenuItem menu = new MenuItem("退出");//创建菜单项
//            menu.addActionListener(new ActionListener() {//给菜单项添加事件监听器，单击时退出系统
//
//                @Override
//                public void actionPerformed(ActionEvent e) {
//                    // TODO Auto-generated method stub
//                    System.exit(0);
//
//                }
//            });
//            pop.add(menu);
//            TrayIcon tray = new TrayIcon(icon.getImage(), "Hern",pop);
//
//            // 获得系统托盘对象
//            SystemTray systemTray = SystemTray.getSystemTray();
//            systemTray.add(tray);// 将托盘图片添加到系统托盘中
//        }
//
//    }

